<template>
  <div class="appointmentList-view">
    <!-- 模块展示 -->
    <p class="fs9 fwbold mb20">模块展示</p>
    <div class="bgf5f6 pb20 pt20 pl10 flex-a-c mb20">
      <el-checkbox-group v-model="checkList" class="mr20">
        <el-checkbox label="动态文章模板"></el-checkbox>
        <el-checkbox label="活动报名模板"></el-checkbox>
      </el-checkbox-group>
      <p class="lineheight0 fs6">
        默认动态文章模块为选中。可单选、可多选，但不可不选。用户端根据选中的状态显示内容。
      </p>
    </div>
    <!-- 商城模块 -->
    <p class="fs9 fwbold mb20">动态模块</p>
    <div class="bgf5f6 pb20 pt20 pl10 flex-a-c mb20" v-if="currentType == 0">
      <p class="lineheight0 fs7 mr20">
        模块名称
      </p>
      <el-input type="text"  
                class="w200 "
                v-model="dynamicName"
                maxlength="6"
                show-word-limit>
      </el-input>
    </div>

    <!-- <div class="d-flex mb20 pl10">
      <p class="fs7 mr20">
        模块样式
      </p>
      <div class="">
        <p class="fs7 mr20 fwbold mb20">
          当前样式：{{templateTitle}}   
        </p>
        <el-button size="small" @click="templateDialog = true" type="primary">更换样式</el-button>
      </div>
      
    </div> -->
    <div v-if="currentType == 0 ">
      <div class="d-flex  pl10">
        <div class="fs7 mr10" style="width:70px">
          顶部轮播图
        </div>
        <div class="d-flex flex-wrap-y" style="width:1440px">

          <div class="bgf7fa pa10 d-flex align-items-center mr20 mb20 posre"  v-for="(item,index) in  mallBanner" :key="index">
            <div class="d-flex flex-column align-items-center ">
              <el-upload
                    accept="image/jpeg,image/png"
                    class="addTabIcon"
                    :action="uploadUrl"
                    :show-file-list="false"
                    :on-success="bannerSuccess.bind(null, {'index':index,'data':item})"
                    :before-upload="beforeUploadImageDynamicPic">
                <img v-if="item.img" :src="item.img" class="avatar dynamic">
                <i v-else class="el-icon-plus"></i>
                
            </el-upload>
            <p class="fs6">建议上传750px * 422px的图片效果最佳</p>
            </div>
            <jumpSet :title="item.title"
                      :index="index"
                      :currentGoods="item"
                      :goodsData="item"
                      @select_tap="select_tap"></jumpSet>
            <i class="el-icon-delete posab fs9 hover_pointer" 
              v-if="index !== 0"
              @click="deleteImg(index)"
              style="bottom:10px;right:10px"></i>
            <el-button size="small"
                      type="primary" 
                      class="posab" 
                      style="right:-110px"
                      @click="addImg"
                      v-if="mallBanner.length - 1  == index &&  index !== 5">
              添加图片
            </el-button>
          </div>
        </div>
        
      </div>
      
      
     
    </div>
    <!-- 预约模块 -->
    <div v-if="!onlyStore">
      <p class="fs9 fwbold mb20">活动模块</p>
      <div class="bgf5f6 pb20 pt20 pl10 flex-a-c mb20">
        <p class="lineheight0 fs7 mr20">
          模块名称
        </p>
        <el-input type="text"  
                  class="w200 "
                  v-model="activityName"
                  maxlength="6"
                  show-word-limit>
        </el-input>
      </div>
      
      <div class="d-flex  pl10">
        <div class="fs7 mr10" style="width:70px">
          顶部轮播图
        </div>
        <div class="d-flex flex-wrap-y" style="width:1440px">

          <div class="bgf7fa pa10 d-flex align-items-center mr20 mb20 posre"  v-for="(item,index) in  appointmentBanner" :key="index">
            <div class="d-flex flex-column align-items-center ">
              <el-upload
                    accept="image/jpeg,image/png"
                    class="addTabIcon"
                    :action="uploadUrl"
                    :show-file-list="false"
                    :on-success="bannerSuccess.bind(null, {'index':index,'data':item,'type':'b'})"
                    :before-upload="beforeUploadImageDynamicPic">
                <img v-if="item.img" :src="item.img" class="avatar dynamic">
                <i v-else class="el-icon-plus"></i>
                
            </el-upload>
            <p class="fs6">建议上传750px * 422px的图片效果最佳</p>
            </div>
            <jumpSet :title="item.title"
                     :index="index"
                     :goodsData="item"
                     :currentGoods="item"
                     :type="'appointment'"
                     @select_tap="select_tap"></jumpSet>
            <i class="el-icon-delete posab fs9 hover_pointer" 
              v-if="index !== 0"
              @click="deleteImg(index,'b')"
              style="bottom:10px;right:10px"></i>
            <el-button size="small"
                      type="primary" 
                      class="posab" 
                      style="right:-110px"
                      @click="addImg('b')"
                      v-if="appointmentBanner.length - 1  == index &&  index !== 5">
              添加图片
            </el-button>
          </div>
        </div>
      </div>

    </div>
    <div class="flex-j-c">
      <el-button size="small"
                type="primary" 
                @click="saveImg">
            保存
      </el-button>
    </div>
  </div>
</template>

<script>
import {
  queryShopModelConfigAll,
  queryShopConfig,
  saveShopConfig
} from "@/api/wechat";
import {
  queryBaseSetting,
  updateBaseSetting
} from "@/api/companyManage.js";
import { getDataTimeSec } from "@/utils";
import jumpSet from "./components/jumpSet";
export default {
  data() {
    return {
      checkList:[],
      dynamicName:'动态文章', //动态文章模板
      activityName:'活动报名', // 活动报名模板
      uploadUrl:this.$store.state.uploadingUrl,
      ossUrl:this.$store.state.ossUrl,
      mallBanner:[{
        img:"https://wanyun-vcard.oss-cn-chengdu.aliyuncs.com/product-index/20190418160834.png",
        title:"无"
      }],
      appointmentBanner:[{
        img:"https://wanyun-vcard.oss-cn-chengdu.aliyuncs.com/product-index/20190418160834.png",
        title:"无"
      }],
      optionList:[
        {
          text: "无",
        },
        {
          text: "名片页",
        },
        {
          text: "媒体页",
        },
        {
          text: "商城页",
        },
        {
          text: "动态页",
        },
        {
          text: "官网页",
        },
      ],
      templateDialog:false, // 商城模板
      currentTitle:'简约模式',
      currentType:0 ,
      templateTitle:'简约模式',
      templateType:0,
      onlyStore:false,
      allConfig:''
    };
  },
  components:{
    jumpSet,
  },
  watch:{
    
  },
  created(){
    this.queryConfig()
    this.queryShopConfigAll()
  },  
  methods:{
    queryConfig(){
      let than = this 
      let data = {
        ignore:1
      }
      queryBaseSetting(data)
      .then(res=>{
        let than = this
        if(!res.data){
          return
        }
        if(JSON.parse(res.data.dynamicTop)){
          than.$set(than,'mallBanner', JSON.parse(res.data.dynamicTop))
        }
        if(JSON.parse(res.data.activityTop)){
          than.$set(than,'appointmentBanner', JSON.parse(res.data.activityTop))
        }
        console.log(this.mallBanner)
        than.dynamicName = res.data.dynamicName
        than.activityName = res.data.activityName
        if(res.data.showActivity){
          than.checkList.push('活动报名模板')
        }
        if(res.data.showDynamic){
          than.checkList.push('动态文章模板')
        }
        console.log(than.checkList)
      })
    },
    queryShopConfigAll(){
      let data = {
        ignore:true
      }
      queryShopModelConfigAll(data)
      .then(res=>{
        this.allConfig = res.data
      })
    },
    addImg(type){
      if(type !== 'b'){
        this.mallBanner.push({
          title:'无',
          img:'',
        })
      }else{
        this.appointmentBanner.push({
          title:'无',
          img:'',
        })
      }
    },
    deleteImg(index,type){
      if(type !== 'b'){
        this.mallBanner.splice(index,1)
      }else{
        this.appointmentBanner.splice(index,1)
      }
    },
    select_tap(index,val,type,goods,apptype){
      if(type !== 'appointment'){//商城
        
        this.mallBanner[index].title = val
        if(goods){
          this.mallBanner[index].goods = goods
          delete this.mallBanner[index].page
        }else{
          delete this.mallBanner[index].goods 
          this.mallBanner[index].page = type
        }
      }else{//预约
        this.appointmentBanner[index].title = val
        if(goods){
          this.appointmentBanner[index].goods = goods
          delete this.appointmentBanner[index].page 
        }else{
          delete this.appointmentBanner[index].goods 
          this.appointmentBanner[index].page = apptype
        }
      }
      
    },
    bannerSuccess(obj,res,file){
      //动态图上传成功
      if(!obj.type){
        var index = obj.index;
        this.$set(this.mallBanner[index],'img',this.ossUrl + '/' + res.data)
      }else{
        var index = obj.index;
        this.$set(this.appointmentBanner[index],'img',this.ossUrl + '/' + res.data)
      }
      
    },
    beforeUploadImageDynamicPic(file){
      var _this = this;
      var isLt10M = file.size / 1024 / 1024  < 10;
      if (['image/jpeg', 'image/png'].indexOf(file.type) == -1) {
          _this.$message.error('请上传正确的图片格式');
          return false;
      }
      if (!isLt10M) {
          _this.$message.error('上传图片大小不能超过10MB哦!');
          return false;
      }
    },
    selectTemplate(item){
      this.currentTitle = item.title
      this.currentType = item.type
    },
    // 保存模板
    saveImg(){
      let than = this
      let marr = JSON.parse(JSON.stringify(than.mallBanner))
      let appArr = JSON.parse(JSON.stringify(than.appointmentBanner))
      let showDynamic = this.checkList.includes('动态文章模板') ? 1 : 0
      let showActivity = this.checkList.includes('活动报名模板') ? 1 : 0
     
      if(!showDynamic && !showActivity){
        this.$message.warning('请先选择至少一个模块')
        return
      }
      let data = {
        activityName:than.activityName,
        activityTop:JSON.stringify(appArr),
        dynamicName:than.dynamicName,
        dynamicTop:JSON.stringify(marr),
        showDynamic:showDynamic,
        showActivity:showActivity,
      }
      console.log(data)
      updateBaseSetting(data)
      .then(res=>{
        than.$message.success("保存成功")
      })
    },
  }
};
</script>

<style lang="scss" scoped>
.appointmentList-view{
  background: #fff;
  padding:30px;
}
 /deep/.addTabIcon .el-upload--text{
  width: 210px;
  height: 100px;
  line-height: 100px;
  margin-bottom: 10px;
}
/deep/ .addTabIcon .el-upload-list__item {
  width: 210px;
  height: 100px;
  line-height: 100px;

}
/deep/ .el-radio__inner{
  width: 20px;
  height: 20px;
}
/deep/ .el-radio__label{
  font-size: 16px;
}
.avatar{
  width: 100%;
  height: 100%;
}
.imgScroll::-webkit-scrollbar { width: 0 !important }
</style>